//
//  ViewController.m
//  CoreAnimationDemo
//
//  Created by 谭鹏 on 2017/4/26.
//  Copyright © 2017年 谭鹏. All rights reserved.
//

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

#import "TPArcView.h"
@interface ViewController ()
//波浪参数
@property CADisplayLink* displayLink;
@property CAShapeLayer * vaveLayer;
@property float offsetX;

//扇形 圆形 参数
@property UIView * arcView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //扇形 可点击
    [self setArc];

    
    
    
    
    
    
    //波浪曲线 动画
//    [self setVaveMove];
    

    
}
#pragma mark - 扇形
-(void)setArc{
//    _arcView = [[UIView alloc]init];
//    [self.view addSubview:_arcView];
//    _arcView.frame = CGRectMake(0, 0, 467, 467);
    
    NSArray<NSString *> * numberTexts = @[@"1",@"2",@"4",@"8",@"16"];
    
    float sum = 0;
    for (NSString * number in numberTexts) {
        sum += number.floatValue;
        
    }
    
    NSMutableArray * colors = [NSMutableArray new];
    for (NSString * number in numberTexts) {
        UIColor * color = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
        [colors addObject:color];
        
    }
//    @property CGFloat lineWidth;
//    @property CGPoint arcCenter;
//    @property CGFloat radius;
//    
//    @property NSMutableArray<NSString *> * numberTexts;
//    @property NSMutableArray<UIBezierPath *> * paths;
//    @property NSMutableArray<UIColor *> * colors;
    TPArcView * arc = [[TPArcView alloc]init];
    arc.frame = CGRectMake(0, 0, 300, 300);
    arc.lineWidth = 50;
    arc.radius = 100;
//    arc.arcCenter = CGPointMake( 150, 150);
    arc.numberTexts = numberTexts;
    arc.colors = colors.copy;
    arc.backgroundColor = [UIColor clearColor];
    [self.view addSubview:arc];
    //------
    
    //点击事件
    
//    UITapGestureRecognizer * tapArcGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapArc:)];
//    [arc addGestureRecognizer:tapArcGR];
    
    
}
-(void)tapArc:(UITapGestureRecognizer *)tapGR{
    TPArcView * arc = (id)tapGR.view;
    CGPoint touchPoint = [tapGR locationInView:self.view];
//    NSLog(@"%@",NSStringFromCGPoint(touchPoint));
//    for (int i = 0; i < [UIScreen mainScreen].bounds.size.height; i ++) {
//        for (int j = 0; j < [UIScreen mainScreen].bounds.size.width; j ++) {
//            CGPoint p = CGPointMake(j, i);
    
//            NSLog(@"%f %f",p.x,p.y);
//            NSLog(@"%@",self.view.layer.sublayers[2].sublayers[0].sublayers);
            NSMutableArray * paths = arc.paths;
            for (int i = 0 ; i <paths.count ; i ++ ) {
                
                UIBezierPath * path = (id)paths[i];
//                NSLog(@"循环到：%@",path);
                if (CGPathContainsPoint(path.CGPath, nil, touchPoint, NO)) {
                    NSLog(@"presentationLayer点击的是：%d",i);
                    break;
                }
                
                

            }
    
//            
//        }
//    }
//    NSLog(@"ok");
    //遍历当前视图上的子视图的presentationLayer 与点击的点是否有交集
    
    
}
#pragma mark - 波浪线
-(void)setVaveMove{
        _vaveLayer = [CAShapeLayer layer];
    [self.view.layer addSublayer:_vaveLayer];
    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(disPlayLink:)];
    [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
/**
 *  绘制曲线
 *
 *  @param A       振幅 px
 *  @param t       周期长度 px
 *  @param h       y轴偏移量
 *  @param offsetX x轴偏移量 一般调整此参数 运动波浪线
 *
 *  @return UIBezierPath
 */
-(UIBezierPath *)getWavePathWithA:(CGFloat)A T:(CGFloat)t H:(CGFloat)h OffsetX:(CGFloat)offsetX{
    //画波浪
    //    CGFloat A = 3;//振幅
    //    CGFloat w = 50;//
    //    CGFloat h = 50 ;//初始位置的高度
    UIBezierPath * path = [UIBezierPath bezierPath];
    

    [path moveToPoint:CGPointMake(0, h)];
    for (int x = 0; x <= UIScreen.mainScreen.bounds.size.width ; x ++) {
        
        CGFloat y =  A * sinf( 2*M_PI*(x + offsetX)/t ) + h;
        
        [path addLineToPoint:CGPointMake(x, y)];
    }
    [path addLineToPoint:CGPointMake(UIScreen.mainScreen.bounds.size.width, 0)];
    [path addLineToPoint:CGPointMake(0, 0)];
    
    return path;
}
- (void)disPlayLink:(CADisplayLink *)displayLink {
    _offsetX += 0.1;
    self.vaveLayer.shouldRasterize = YES;
    self.vaveLayer.path = [self getWavePathWithA:20 T:200 H:60 OffsetX:_offsetX].CGPath;
}


@end
